package cn.com.suhenry.controller;


import cn.com.suhenry.pojo.User;
import cn.com.suhenry.service.UserService;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/login")
    public String login(User user, HttpSession session,Model model){
        User user1 = userService.findUserByName(user.getName());
        if (user1.getName()==null){
            return "submit";
        }else if (user.getName().equals(user1.getName()) && user.getPassword().equals(user1.getPassword())){
            session.setAttribute("USER",user1);
            return "index";
        }else{
            model.addAttribute("账号或密码错误","warn");
            return "login";
        }
    }
    @GetMapping("/submit")
    public String submit(User user){
        User user1 = userService.findUserByName(user.getName());
        if (user1!=null){
            //已被注册
            return "submit";
        }else{
            user.setRole("USER");
            userService.insertUser(user);
            return "login";
        }

    }
}
